package com.smarth.zhizulian.http.utils;

import android.content.Context;
import android.text.TextUtils;

import com.smarth.zhizulian.app.BaseConfig;
import com.smarth.zhizulian.bean.cash.OldBaseEntity;
import com.smarth.zhizulian.entity.EventBean;
import com.smarth.zhizulian.utils.GsonUtils;
import com.smarth.zhizulian.utils.KumaToast;

import org.simple.eventbus.EventBus;

import java.io.IOException;

import retrofit2.HttpException;

/**
 * Created by 18865 on 2017/12/29.
 * 老数据源暂时处理
 */

public class OldDataUtils {
    public static void handleDataError(Context context, Throwable e) {
        try {
            if (e instanceof HttpException) {
                HttpException he = (HttpException) e;
                try {
                    String json = he.response().errorBody().string();
                    if (he.code() != 404) {
                        OldBaseEntity old = GsonUtils.fromJson(json, OldBaseEntity.class);
                        if (he.code() == 400) {
                            if (old.getCode() == 5105) {
                                EventBus.getDefault().post(new EventBean(old.getMessage()), BaseConfig.EventTag.RELOGIN);
                            } else {
                                KumaToast.show(context, old.getMessage());
                            }
                        } else {
                            KumaToast.show(context, old.getMessage());
                        }
                    } else {
                        KumaToast.show(context, e.getMessage());
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                    KumaToast.show(context, e.getMessage());
                }
            } else {
                KumaToast.showError(context, e.getMessage());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            KumaToast.showError(context, ex.getMessage());
        }
    }
}
